package com.itheima.edu.info.manager.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class DateUtil {

    private DateUtil(){}

    public static int getAgeByBirthday(String birthday) {
        try{
            //1. 创建SimpleDateFormat 对象
            SimpleDateFormat spf = new SimpleDateFormat("yyyy-MM-dd");
            //2. 将传入的生日字符串解析成对应的Date对象
            Date parse = spf.parse(birthday);
            //3. 获取当前系统时间
            Date d = new Date();
            //4. 计算两个日期间的差值
            long time1 = parse.getTime();
            long time2 = d.getTime();
            //5. 通过毫秒值计算年龄
            int age = (int)((time2 - time1) / 1000 / 3600 / 24 / 365);
            return age;
        }catch(ParseException p){
            p.printStackTrace();
        }
        return 0;
    }
}
